﻿///////////////////////////////////////////////////////
//
// XDesigner.ORM 
//
// autohr : yuans
//
// date: 2011-2-23
//
// email  : yyf9989@hotmail.com
// 
//
///////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace XDesigner.ORM
{
    /// <summary>
    /// Data base operation layer, handle insert,delete,update and select command.
    /// </summary>
    public class ORMDataBase : IDisposable
    {
        private IDbConnection _Connection = null;
        /// <summary>
        /// Data base connection instance
        /// </summary>
        public IDbConnection Connection
        {
            get { return _Connection; }
            set { _Connection = value; }
        }

        [System.ComponentModel.Browsable( false )]
        public bool IsOpen
        {
            get
            {
                return this.Connection != null && this.Connection.State == ConnectionState.Open ;
            }
        }

        public bool Open()
        {
            if (this.Connection != null)
            {
                if (this.Connection.State != ConnectionState.Open)
                {
                    this.Connection.Open();
                }
                return true;
            }
            return false;
        }

        public void Close()
        {
            if (this.Connection != null)
            {
                if (this.Connection.State == ConnectionState.Open)
                {
                    this.Connection.Close();
                }
            }
        }

        public int ExecuteNonQuery(ORMDBCommand cmd)
        {
            CheckConnection();
            using (IDbCommand dbcmd = cmd.CreateCommand(this.Connection))
            {
                int result = dbcmd.ExecuteNonQuery();
                return result;
            }
        }

        public object ExecuteScalar(ORMDBCommand cmd)
        {
            CheckConnection();
            using (IDbCommand dbcmd = cmd.CreateCommand(this.Connection))
            {
                object result = dbcmd.ExecuteScalar();
                return result;
            }
        }

        public Array ReadAllValues(ORMDBCommand cmd)
        {
            CheckConnection();
            using (IDbCommand dbcmd = cmd.CreateCommand(this.Connection))
            {
                IDataReader reader = dbcmd.ExecuteReader(CommandBehavior.SingleResult);
                System.Collections.ArrayList result = new System.Collections.ArrayList();
                int fieldCount = reader.FieldCount;
                while (reader.Read())
                {
                    for (int iCount = 0; iCount < fieldCount; iCount++)
                    {
                        result.Add(reader.GetValue(iCount));
                    }
                }
                reader.Close();
                return result.ToArray();
            }
        }

        public IDbCommand CreateCommand(ORMDBCommand cmd)
        {
            CheckConnection();
            return cmd.CreateCommand(this.Connection);
        }

        public IDbCommand CreateCommand()
        {
            CheckConnection();
            return this.Connection.CreateCommand();
        }

        private void CheckConnection()
        {
            if (_Connection == null || _Connection.State != ConnectionState.Open)
            {
                throw new InvalidOperationException("Connection isnot opend");
            }
        }

        #region IDisposable 成员

        public void Dispose()
        {
            if (_Connection != null)
            {
                _Connection.Close();
                _Connection.Dispose();
                _Connection = null;
            }
        }

        #endregion
    }
}
